package com.xuecheng.content.api;

import com.xuecheng.base.exception.BusinessException;
import com.xuecheng.base.exception.CommonError;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.TeachplanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class TeachplanController {

    @Autowired
    private TeachplanService  teachplanService;


    /**
     * 根据课程id查询课程计划
     * @param courseId
     * @return
     */
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public List<TeachplanDto> getTeachPlan(@PathVariable Long courseId){
        return teachplanService.getTeachPlan(courseId);
    }

    /**
     * 新增或修改章节
     * @param teachplan
     */
    @ApiOperation("课程计划创建或修改")
    @PostMapping("/teachplan")
    public void saveTeachplan( @RequestBody SaveTeachplanDto teachplan){
        teachplanService.saveTeachplan(teachplan);
    }

    /**
     * 删除章节
     * @param teachplanId
     */
    @ApiOperation("课程计划创建或修改")
    @DeleteMapping("/teachplan/{teachplanId}")
    public void deleteTeachplan( @PathVariable Long teachplanId){
        if (teachplanId == null || teachplanId <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        teachplanService.deleteTeachplan(teachplanId);
    }

    /**
     * 章节下移
     * @param teachplanId
     */
    @PostMapping("teachplan/movedown/{teachplanId}")
    public void teachplanMoveDown(@PathVariable("teachplanId") long teachplanId){
        if (teachplanId <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        teachplanService.moveDown(teachplanId);
    }

    /**
     * 章节上移
     * @param teachplanId
     */
    @PostMapping("teachplan/moveup/{teachplanId}")
    public void teachplanMoveUp(@PathVariable("teachplanId") long teachplanId){
        if (teachplanId <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        teachplanService.moveUp(teachplanId);
    }


    @ApiOperation(value = "课程计划和媒资信息绑定")
    @PostMapping("/teachplan/association/media")
    public void associationMedia(@RequestBody BindTeachplanMediaDto bindTeachplanMediaDto){
        teachplanService.binidTeachplanMedia(bindTeachplanMediaDto);
    }

}
